<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js表单</title>
</head>
<body>
    <form name="myForm" action="demo_form.html" onsubmit="return validateForm()" method="post">
       name: <input type="text" name="fname">
        <input type="submit" value="submit">
    </form>
    <script>
        function validateForm(){
            var x = document.forms["myForm"]["fname"].value;
            if(x == null |x == ""){
                alert("please input name!")
                return false;
            }
        }
    </script>
    <!-- ________________________________________________________________________________________ -->
    <h1>JavaScript 验证输入</h1>
    <p>请输入 1 到 10 之间的数字：</p>
    <form name="form1">
        <input type="text" name="fname1">
        <button type="button" value="submit" onclick="function1()">submit</button>
    </form>
    <script>
        function function1() {
            var str;
            var value = document.forms["form1"]["fname1"].value;
            if(value==null | value==""){
                alert("please input number!")
            }else if(value<1|value>10){
                document.write("input error!")
            }else{
                document.write(value);
            }
           
            return ;
        }
    </script>
</body>
</html>